#include "app_rs485.h"
#include "bsp_usart.h"
#include "systick.h"
#include "main.h"

RS485_t rs485;

void app_485_state_init(void)
{

    for (uint8_t i = 0; i < 100; i++)
    {
        rs485.tx_buffer[i] = i;
    }

    for (uint8_t i = 0; i < 100; i++)
    {
        rs485.rx_buffer[i] = 0;
    }
    
}

void app_485_run(void)
{
    static uint8_t step = 0;
    static uint8_t cnt = 0;

    switch (step)
    {
        case 0:
        cnt++;
        if(cnt>=SYS_HEART_MS(50))
        {
            cnt = 0;
            step = 1;
            dv_UART.fRxPinCtrl(true);
        }        
        break;

        case 1:
        if(dv_UART.u8rx_ok == true && dv_UART.u8UartRecvCnt>=100)
        {
            dv_UART.fread_data(rs485.rx_buffer,1);
            dv_UART.u8rx_ok = false;
            step = 2;
        }        
        break;

        case 2:
        dv_UART.fRxPinCtrl(false);
        step = 3;
        break;

        case 3:
        cnt++;
        if(cnt>=SYS_HEART_MS(100))
        {
            cnt = 0;
            dv_UART.fwrite_data(rs485.rx_buffer,100);
            step = 0;
        } 
        break;

        default:
        break;
    }
}